      1  import java.awt.Color;

      2

      3  public class MaxDemo

      4  {

      5     public static <E extends Comparable> E max(E[] a)

      6     {

      7        E largest = a[0];

      8        for (int i = 1; i < a.length; i++)

      9        {

     10           if (a[i].compareTo(largest) > 0)

     11           {

     12              largest = a[i];

     13           }

     14        }

     15        return largest;

     16     }

     17

     18     public static void main(String[] args)

     19     {

     20        String[] words = { "Mary", "had", "a", "little", "lamb" };

     21

     22        Color[] colors = { Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW };

     23

     24        Integer[] squares = { 1, 4, 9, 16, 25, 36 };

     25

     26        System.out.println(max(words)); // Calls max<String>

     27        // System.out.println(max(colors)); // Error: Color is not Comparable

     28        System.out.println(max(squares)); // Calls max<Integer>

     29     }

     30

     31  }